namespace("sg");
sg.Loader = {
    suspendResCount:0,
    listeners: {},
    init: function() {
        this.loadImages();
    },
    loadImages: function(images) {
        var context = this;
        for(var i in images) {
            this.suspendResCount++;
            var _img = images[i].image = new Image();
            _img.onload = function(){
                context.suspendResCount--;
                if(context.suspendResCount == 0) {
                    context.onEvent("onload", images);
                }
            }
            _img.src = images[i].src;
        }
    },
    addEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName].push(clback);
    },
    removeEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName].splice(this.listeners[eventName].indexOf(clback), 1);
    },
    onEvent: function(eventName, obj) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        for(var i=0; i<this.listeners[eventName].length; i++) {
            this.listeners[eventName][i](obj);
        }
    }
};
